{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "def sort_priority(values, group):\n",
    "    def helper(x):\n",
    "        if x in group:\n",
    "            return (0, x)\n",
    "        return (1, x)\n",
    "    values.sort(key=helper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 1, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "numbers = [8, 3, 1, 2, 5, 4, 7, 6]\n",
    "group = {2, 3, 5, 7}\n",
    "sort_priority(numbers, group)\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "def sort_priority2(numbers, group):\n",
    "    found = False\n",
    "    def helper(x):\n",
    "        if x in group:\n",
    "            found = True  # Seems simple\n",
    "            return (0, x)\n",
    "        return (1, x)\n",
    "    numbers.sort(key=helper)\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found: False\n",
      "[2, 3, 5, 7, 1, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "found = sort_priority2(numbers, group)\n",
    "print('Found:', found)\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 5\n",
    "def sort_priority2(numbers, group):\n",
    "    found = False         # Scope: 'sort_priority2'\n",
    "    def helper(x):\n",
    "        if x in group:\n",
    "            found = True  # Scope: 'helper' -- Bad!\n",
    "            return (0, x)\n",
    "        return (1, x)\n",
    "    numbers.sort(key=helper)\n",
    "    return found"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### nonlocal?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "def sort_priority3(numbers, group):\n",
    "    found = False\n",
    "    def helper(x):\n",
    "        nonlocal found\n",
    "        if x in group:\n",
    "            found = True\n",
    "            return (0, x)\n",
    "        return (1, x)\n",
    "    numbers.sort(key=helper)\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found: True\n",
      "[2, 3, 5, 7, 1, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# Example 7\n",
    "found = sort_priority3(numbers, group)\n",
    "print('Found:', found)\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found: True\n",
      "[2, 3, 5, 7, 1, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# Example 8\n",
    "class Sorter(object):\n",
    "    def __init__(self, group):\n",
    "        self.group = group\n",
    "        self.found = False\n",
    "\n",
    "    def __call__(self, x):\n",
    "        if x in self.group:\n",
    "            self.found = True\n",
    "            return (0, x)\n",
    "        return (1, x)\n",
    "\n",
    "sorter = Sorter(group)\n",
    "numbers.sort(key=sorter)\n",
    "assert sorter.found is True\n",
    "print('Found:', found)\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found: True\n",
      "[2, 3, 5, 7, 1, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# Example 9\n",
    "def sort_priority(numbers, group):\n",
    "    found = [False]\n",
    "    def helper(x):\n",
    "        if x in group:\n",
    "            found[0] = True\n",
    "            return (0, x)\n",
    "        return (1, x)\n",
    "    numbers.sort(key=helper)\n",
    "    return found[0]\n",
    "\n",
    "numbers = [8, 3, 1, 2, 5, 4, 7, 6]\n",
    "group = set([2, 3, 5, 7])\n",
    "found = sort_priority(numbers, group)\n",
    "print('Found:', found)\n",
    "print(numbers)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
